<?php
    error_reporting(0); 
    require_once("condb.php");     
    require_once("functions/functions_global_card.php");     
    require_once('libs/nusoap.php');
    
    $server = new soap_server();
    
    $ns = "urn:ltbc";
    $server->configureWSDL("ltbc", $ns);
    $server->wsdl->schemaTargetNamespace = $ns;             
       
    $server->register(
        "call"
        , array("game_user"=>"xsd:string", "provider_code"=>"xsd:string", "card_code"=>"xsd:string", "card_seri"=>"xsd:string", "token"=>"xsd:string", "test"=>"xsd:string")        
        , array("return"=> "xsd:string")
        , $ns
        , $ns . "#call"
        , "rpc"            
        , "encoded"
        , "Examination Results"  
    );
    
    function GetUserByGameUser($game_user)
    {
        $sql = "SELECT userId FROM c_ltbc WHERE `game_user`='" . mysql_escape_string($game_user) . "'";
        $result = mysql_query($sql);
        $rows = mysql_fetch_assoc($result);
        return $rows;
    } 
    
    function call($game_user, $provider_code, $card_code, $card_seri, $token, $test)
    {
        $gameId = 174;
        $partnerId = 60;
        
        $res = array();
        
        /* Lay userId */
        $user = GetUserByGameUser($game_user);
        if(!empty($user)){
            $data["userId"] = $user["userId"];
            $data["gameId"] = $gameId;
            $data["game_user"] = $game_user;
            $data["provider_code"] = $provider_code;
            $data["card_code"] = $card_code;
            $data["card_seri"] = $card_seri;        
            $data["partnerId"] = $partnerId;
            $data["requested_date"] = time();
            
            if($token=="0ec0c7e31007031149ff6835843b3dca"){
                
                $response = callService($data);
                
                $data["response"] = removeSign($response->response);
                $data["price"] = $response->price;
                $data["status"] = $response->status==1 ? 1 : 0;                        
                $data["response_status"] = $response->response_status;
                
                $res["status"] = $data["status"];
                $res["msg"] = $data["status"]==1 ? $data["price"] : $data["response"];
                
                /* tỷ lệ ăn chia */
                $percent = getPercent($data["gameId"]);
                
                $data["percent_wapmaster"] = $percent["percent_wapmaster"];
                $data["percent_partner"] = $percent["percent_partner"];
                $data["transactionId"] = round(microtime(true) * 10000);
                $data["responsed_date"] = time();
                 
                if($test=="0"){
                    $result = InsertCardLog($data);       
                } else {                
                    $result = InsertCardLogTest($data);                   
                }
                
            } else {
                $res["status"] = 0;
                $res["msg"] = "Invalid Request";
            } 
        } else {
            $res["status"] = 0;
            $res["msg"] = "User khong ton tai";   
        }
        return $res["status"] . "|" . $res["msg"];    
    }
    
    function callService($data)
    {
        $client = new SoapClient("http://192.168.1.127:8008/hdc_payment/hdc_pcard_service.php?wsdl");
           
        $content = array();
        $content["username"] = $data["game_user"];
        $content["email_user"] = "";
        $content["mobile"] = "";
        $content["card_code"] = $data["card_code"];
        $content["card_seri"] = $data["card_seri"];
        $content["provider_card"] = $data["provider_code"];
        $content["provider_payment"] = "epay";
        $content["service_type"] = "taoviec";
        $content["partner_id"] = $data["partnerId"];
        $content["refcode"] = $data["userId"];
        $content["content_id"] = $data["gameId"];        
        
        $response = $client->__call("wsCard", $content);
        $response = json_decode($response);
        
        return $response;
    }            
    
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ""; 
    $server->service($HTTP_RAW_POST_DATA);
    exit();
?>